using AiQiuQuan.Sport.Model.Enums;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace AiQiuQuan.Sport.Model.Models
{
    /// <summary>
    /// 新增约球dto
    /// </summary>
    public class BallAddDto : IValidatableObject
    {
        /// <summary>
        /// 名称
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "名称必填")]
        [StringLength(200, ErrorMessage = "名称不能超过200个字符")]
        public string Name { get; set; }

        /// <summary>
        /// 1一次、2定期
        /// </summary>
        [Required(ErrorMessage = "1一次、2定期必填")]
        [EnumDataType(typeof(BallTypeEnum), ErrorMessage = "1一次、2定期")]
        public BallTypeEnum Type { get; set; }

        /// <summary>
        /// 场馆ID
        /// </summary>
        [Required(ErrorMessage = "场馆ID必填")]
        public Guid VenueId { get; set; }

        /// <summary>
        /// 场地名称
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "场地名称必填")]
        [StringLength(100, ErrorMessage = "场地名称不能超过100个字符")]
        public string SiteName { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        [Required(ErrorMessage = "开始日期必填")]
        public DateTimeOffset StartDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTimeOffset? EndDate { get; set; }

        /// <summary>
        /// 开始时间:08:00
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "开始时间:08:00必填")]
        [StringLength(10, ErrorMessage = "开始时间:08:00不能超过10个字符")]
        [RegularExpression("^\\d{2}:\\d{2}$", ErrorMessage = "格式:08:00")]
        public string StartTime { get; set; }

        /// <summary>
        /// 结束时间:23:00
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "结束时间:23:00必填")]
        [StringLength(10, ErrorMessage = "结束时间:23:00不能超过10个字符")]
        [RegularExpression("^\\d{2}:\\d{2}$", ErrorMessage = "格式:23:00")]
        public string EndTime { get; set; }

        /// <summary>
        /// 0 Sunday、1 Monday、2 Tuesday、3 Wednesday、4 Thursday、5 Friday、6 Saturday
        /// </summary>
        public DayOfWeek? WeekDay { get; set; }

        ///// <summary>
        ///// 报名最大队伍数
        ///// </summary>
        //[Required(ErrorMessage = "报名最大队伍数必填")]
        //[Range(2, 100, ErrorMessage = "人数最少为2")]
        //public int LimitCount { get; set; }

        /// <summary>
        /// 1单打、2双打
        /// </summary>
        [Required(ErrorMessage = "1单打、2双打必填")]
        public GameVersusEnum VersusType { get; set; }

        /// <summary>
        /// 金额
        /// </summary>
        [Required(ErrorMessage = "金额必填")]
        [Range(0.01, 999999999, ErrorMessage = "金额小于0.01")]
        public decimal Amount { get; set; }

        /// <summary>
        /// 0男、1女、2混合、3不限
        /// </summary>
        [Required(ErrorMessage = "0男、1女、2混合、3不限必填")]
        public GameSexEnum Sex { get; set; }

        /// <summary>
        /// 起始网球水平
        /// </summary>
        [Required(ErrorMessage = "起始网球水平必填")]
        [Range(1, 20, ErrorMessage = "网球水平小于1")]
        public decimal StartLevel { get; set; }

        /// <summary>
        /// 结束网球水平
        /// </summary>
        [Required(ErrorMessage = "结束网球水平必填")]
        [Range(1, 20, ErrorMessage = "网球水平小于1")]
        public decimal EndLevel { get; set; }

        /// <summary>
        /// 备注说明
        /// </summary>
        [StringLength(500, ErrorMessage = "备注说明不能超过500个字符")]
        public string? Remark { get; set; }

        /// <summary>
        /// 组织者
        /// </summary>
        public string? Host { get; set; }

        /// <summary>
        /// 组织者电话
        /// </summary>
        public string? Phone { get; set; }

        /// <summary>
        /// 组织者图片
        /// </summary>
        public string? Logo { get; set; }

        /// <summary>
        /// 是否平台
        /// </summary>
        [JsonIgnore]
        public bool IsPlatform { get; set; }

        /// <summary>
        /// validate
        /// </summary>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            StartDate = StartDate.Date;
            if (EndDate.HasValue)
            {
                EndDate = EndDate.Value.Date;
            }

            if (Type == BallTypeEnum.Duration && !EndDate.HasValue)
            {
                yield return new ValidationResult("定期约球，结束日期为空");
                yield break;
            }

            if (TimeSpan.Parse(StartTime) >= TimeSpan.Parse(EndTime))
            {
                yield return new ValidationResult("开始时间大于结束时间");
                yield break;
            }

            if (VersusType != GameVersusEnum.Single && VersusType != GameVersusEnum.Double)
            {
                yield return new ValidationResult("对阵类型不正确");
                yield break;
            }

            //if (VersusType == GameVersusEnum.Double && LimitCount != 4)
            //{
            //    yield return new ValidationResult("双打人数固定为4");
            //    yield break;
            //}
            //else if (VersusType == GameVersusEnum.Single && LimitCount != 2)
            //{
            //    yield return new ValidationResult("单打人数固定为2");
            //    yield break;
            //}

            if (StartLevel >= EndLevel)
            {
                yield return new ValidationResult("起始网球水平大于截止网球水平");
                yield break;
            }
        }
    }
}
